﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using Abhiruchi_Phase1.BAL_DAL.ORG_DAL;
using System.Text.RegularExpressions;

namespace Abhiruchi_Phase1
{
    /// <summary>
    /// Interaction logic for ORG_LocationMaster.xaml
    /// </summary>
    public partial class ORG_LocationMaster : Window
    {
        static string id = "0";
         
        public ORG_LocationMaster()
        {
            InitializeComponent();            

            // To Bind Location Master Data Into That Telerik Grid Controle
            BindLocationGridData();

            // To Load Location Incharge Name Insted of Employee Id 
            LoadLocInchargeName();

            // To Increment The Max Location Id Count
            LocationIdCount();
        }

        public void BindLocationGridData()
        {
            DataTable dt = new DataTable();
            dt = LocationMasterDAL.GetORGLoactionInchargeName();            
            radGridLocData.ItemsSource = dt;            
        }

        public void LoadLocInchargeName()
        {
            DataTable dtEName = new DataTable();
            dtEName = LocationMasterDAL.GetORGLoactionENames();
            cmbLocInchrge.DataContext = dtEName.DefaultView;
            cmbLocInchrge.DisplayMemberPath = dtEName.Columns["EName"].ToString();
            cmbLocInchrge.SelectedValuePath = dtEName.Columns["EId"].ToString();
        }

        public void LocationIdCount()
        {
            DataTable mydt = new DataTable();
            mydt = LocationMasterDAL.GetORGLoactionId();
            if (mydt.Rows.Count == 0)
            {
                lblLocId.Content = Int32.Parse(mydt.Rows[0][0].ToString());
            }
            else
            {
                lblLocId.Content = Int32.Parse(mydt.Rows[0][0].ToString()) + 1;
            }
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (Validations())
            {
                 MessageBoxResult msgRes = MessageBox.Show("Do you want to Save this Location Master Information", "Confirmation", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

                 if (msgRes == MessageBoxResult.Yes)
                 {
                     int chk;
                     if (chkIsActive.IsChecked == true)
                     {
                         chk = 1;
                     }
                     else
                     {
                         chk = 0;
                     }
                     if (id == "0")
                     {
                         LocationMasterDAL.GetORGLocationMasterInsert(txtLocName.Text, txtAddress.Text, cmbLocInchrge.SelectedValue.ToString(), txtContactNo.Text, chk);
                         MessageBox.Show("Information Inserted Sucessfully", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
                         ClearFields();
                         LocationIdCount();
                     }
                     else
                     {
                         LocationMasterDAL.GetLocationMasterUpdate(Int32.Parse(lblLocId.Content.ToString()), txtLocName.Text, txtAddress.Text, cmbLocInchrge.SelectedValue.ToString(), txtContactNo.Text, chk);
                         MessageBox.Show("Information Updated Sucessfully", "Confirmation", MessageBoxButton.OK, MessageBoxImage.Information);
                         ClearFields();
                         LocationIdCount();
                         id = "0";
                     }
                     BindLocationGridData();
                 }
            }
        }

        private void radGrdData_RowActivated(object sender, Telerik.Windows.Controls.GridView.RowEventArgs e)
        {
            id = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();            
            lblLocId.Content = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[0])).Value.ToString();
            txtLocName.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[1])).Value.ToString();
            txtAddress.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[2])).Value.ToString();
            cmbLocInchrge.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[3])).Value.ToString();
            txtContactNo.Text = ((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[4])).Value.ToString();
            if (((Telerik.Windows.Controls.GridView.GridViewCell)(e.Row.Cells[5])).Value.ToString() == "No")
            {
                chkIsActive.IsChecked = false;
            }
            else
            {
                chkIsActive.IsChecked = true;
            }            
        }

        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            ClearFields();
            LocationIdCount();
        }

        private void ClearFields()
        {            
            txtLocName.Text = "";
            txtAddress.Text = "";
            cmbLocInchrge.SelectedIndex = -1;
            txtContactNo.Text = "";
            chkIsActive.IsChecked = true;
            radGridLocData.SelectedItem = "";
            id = "0";
        }

        #region Validation
        private bool Validations()
        {
            if (txtLocName.Text == "")
            {
                MessageBox.Show("Please Enter Location Name", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                txtLocName.Focus();
                return false;
            }
            if (txtAddress.Text == "")
            {
                MessageBox.Show("Please Enter Address", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                txtAddress.Focus();
                return false;
            }
            if (cmbLocInchrge.SelectedIndex == -1)
            {
                MessageBox.Show("Please Select Location Incharge Name", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                cmbLocInchrge.Focus();
                return false;
            }
            if (txtContactNo.Text == "")
            {
                MessageBox.Show("Please Enter Contact Number", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                txtContactNo.Focus();
                return false;
            }
            else if (txtContactNo.Text != "")
            {
                if (!Regex.Match(txtContactNo.Text, @"^[-+]?[0-9]*\.?[0-9]+$").Success)
                {
                    MessageBox.Show("Please Enter Valid Contact Number", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
                    txtContactNo.Focus();
                    return false;
                }
            }           
            return true;
        }
        #endregion        

        private void txtLocName_LostFocus(object sender, RoutedEventArgs e)
        {
            
            if (txtLocName.Text != "")
            {
                DataTable dt = new DataTable();
                dt = LocationMasterDAL.GetORGLoactionNameCompare(Int32.Parse(lblLocId.Content.ToString()), txtLocName.Text);
                if (!(dt.Rows[0][0].ToString() == "0"))
                {
                    MessageBox.Show("Location " + txtLocName.Text + " Already Utilized, Please Give Different Location Name", "Duplicate Location Name", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtLocName.Text = "";
                    txtLocName.Focus();                    
                }
            }
        }
    }
}
